package com.tengju.support.infrastructure.persistence.mybatis.mapper.tengju.custom;

import com.tengju.support.infrastructure.persistence.mybatis.dataobject.ArticleDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

    String TEMPLATE_RESULT_MAP = "common_article";

    @Insert({
            "insert into common_article (article_id, title, type, context, create_by, update_by, create_time, update_time)",
            "values (#{articleId}, #{title}, #{type}, #{context}, #{createBy}, #{updateBy}, #{createTime}, #{updateTime})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "articleId")
    int insert(ArticleDO articleDO);

    @Select("select * from common_article where article_id = #{articleId}")
    @Results(id = TEMPLATE_RESULT_MAP,
            value = {
                    @Result(column = "article_id", property = "articleId"),
                    @Result(column = "title", property = "title"),
                    @Result(column = "type", property = "type"),
                    @Result(column = "context", property = "context"),
                    @Result(column = "create_by", property = "createBy"),
                    @Result(column = "update_by", property = "updateBy"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime"),
                    @Result(column = "view_cnt", property = "viewCnt")
            })
    ArticleDO getById(@Param("articleId") Long articleId);


    @Update({
            "update common_article set title = #{title}, type = #{type}, context = #{context},update_by = #{updateBy}"
                    +
                    " where article_id = #{articleId}",
    })
    int updateContext(ArticleDO articleDO);

    @Delete("delete from common_article where article_id = #{articleId}")
    int delete(@Param("articleId") Long articleId);


    @Select("<script>"
            + "     SELECT count(*) FROM common_article"
            + "    where 1=1"
            + "    <if test=\"articleId!=null and articleId >0\">"
            + "      and article_id = #{articleId,jdbcType=BIGINT}"
            + "    </if>"
            + "    <if test=\"title!=null and title !=''\">"
            + "      and title = #{title,jdbcType=VARCHAR}"
            + "    </if>"
            + "    <if test=\"type!=null and type >0\">"
            + "      and type = #{type,jdbcType=INTEGER}"
            + "    </if>"
            + "</script>")
    int count(@Param("articleId") Long articleId, @Param("title") String title,
            @Param("type") Integer type);

    @Select("<script>"
            + "     SELECT * FROM common_article"
            + "    where 1=1"
            + "    <if test=\"articleId!=null and articleId >0\">"
            + "      and article_id = #{articleId,jdbcType=BIGINT}"
            + "    </if>"
            + "    <if test=\"title!=null and title !=''\">"
            + "      and title = #{title,jdbcType=VARCHAR}"
            + "    </if>"
            + "    <if test=\"type!=null and type >0\">"
            + "      and type = #{type,jdbcType=INTEGER}"
            + "    </if>"
            + "    ORDER BY create_time DESC"
            + "    LIMIT #{offset},#{pageSize}"
            + "</script>")
    @ResultMap(value = TEMPLATE_RESULT_MAP)
    List<ArticleDO> queryByPage(@Param("articleId") Long articleId, @Param("title") String title,
            @Param("type") Integer type, @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);

    @Update("update common_article set view_cnt = view_cnt + 1 where article_id = #{articleId} limit 1")
    int updateViewCnt(@Param("articleId") Long articleId);
}
